﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions;
using CrystalDecisions.Enterprise;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
using TI_BAL;
using CrystalDecisions.Shared;

namespace TInventory.Reports.Datasets
{
    public partial class ReportFormInvoice : Form
    {
        TIEntities dbContext;
        public Form parentForm;

        public ReportFormInvoice()
        {
            InitializeComponent();
            dbContext = new TIEntities();
       
        }

       

        private void ReportFormInvoice_Load(object sender, EventArgs e)
        {
            
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

            try
            {
                int invoiceId = int.Parse(this.textBoxInvoice.Text);
                var InvoiceReport = (from j in dbContext.SalesTransactions.Include("ProductType").Include("ProductSubType").Include("ProductSize")
                                     where j.INVOICE_ID == invoiceId
                                     select new
                                     {
                                         TypeName = j.ProductType.Name,
                                         SubTypeName = j.ProductSubType.Name,
                                         SizeName = j.ProductSize.Name,
                                         PER_UNIT_PRICE = j.PER_UNIT_PRICE.Value,
                                         QUANTITY = j.QUANITY.Value,
                                         INVOICE_ID = j.INVOICE_ID.Value,
                                         CustomerCompanyName = j.Customer.CustomerCompanyName
                                     }).ToList();


                //SupplierReport reportobj = new SupplierReport();
                InvoiceReport reportobj = new InvoiceReport();

                reportobj.SetDataSource(InvoiceReport);
                ParameterFieldDefinitions crParameterFieldDefinitions;
                ParameterFieldDefinition crParameterFieldDefinition;
                ParameterValues crParameterValues = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition = crParameterFieldDefinitions[0];
                crParameterValues = crParameterFieldDefinition.CurrentValues;

                crParameterValues.Clear();
                crParameterValues.Add(crParameterDiscreteValue);
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                ParameterFieldDefinitions crParameterFieldDefinitions1;
                ParameterFieldDefinition crParameterFieldDefinition1;
                ParameterValues crParameterValues1 = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                crParameterValues1.Clear();
                crParameterValues1.Add(crParameterDiscreteValue1);
                crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);
                this.crystalReportViewer1.ReportSource = reportobj;
            }

            catch
            {
                MessageBox.Show("Some error occured");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InvoiceListing reportForm = new InvoiceListing();
            reportForm.parentForm = parentForm;
            reportForm.MdiParent = parentForm;
            reportForm.Show();

            reportForm.WindowState = FormWindowState.Minimized;
            reportForm.WindowState = FormWindowState.Maximized;
        }
    }
}
